<?php
namespace Act\Os;
use Lib\ErrorMessage;
use Lib\Common;
class Profile extends Backend{
	public function doGet(){
		$this->display('index.php',array(
			"change_password" => '',
			"user_info" => session('user_info'),
			"quicknoteOptions" => array("1"=>"显示","0"=>"不显示"),
		));
	}

	public function doPost(){
		$user_name = $password = $real_name = $mobile = $email = $user_desc = $change_password = $show_quicknote = $old = $new= '';
		array_walk_recursive($_POST, function(&$v,$k){$v=htmlspecialchars($v);});
		extract($_POST,EXTR_IF_EXISTS);
		$user_mod = model('#User','adm_');
		$user_info = session('user_info');
		$current_user_id = $user_info['user_id'];

		if($change_password){
			$ret=$user_mod->checkPassword($user_info['user_name'],$old);
			if($ret){
				if(strlen($new)<6){
					$this->alert("error",ErrorMessage::PWD_TOO_SHORT);
				}else{
					$user_data['password']=$new;
					$user_mod->updateUser ( $current_user_id, $user_data );
					$this->sysLog( $user_info['user_name'], 'MODIFY', 'User' ,$current_user_id );
					return $this->success (ErrorMessage::PWD_UPDATE_SUCCESS,'/os/index');
				}
			}else{
				$this->alert("error",ErrorMessage::OLD_PWD_WRONG);
			}
		}else{
			$user_data['real_name']=$real_name;
			$user_data['mobile']=$mobile;
			$user_data['email']=$email;
			$user_data['user_desc']=$user_desc;
			$user_data['show_quicknote']=$show_quicknote;
			
			$user_mod->updateUser ( $current_user_id, $user_data );
			
			$user_mod->reloadSession();
			return $this->success ('资料修改成功','/os/index');
		}

		$this->display('index.php',array(
			"change_password" => $change_password,
			"user_info" => $user_info,
			"quicknoteOptions" => array("1"=>"显示","0"=>"不显示"),
		));
	}
}